package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    private OrderService orderService;


    //获取时间区域内到诊人数和预约人数
    @RequestMapping("/findOrderDateValue")
    public Result findOrderDateValue(String value1){
        Map<String, Object> map = new HashMap<>();
        String[] split = value1.split(",");
        //拿到每天的日期
        List<String> dayList = DateUtils.findDaysStr(split[0], split[1]);
        map.put("dayList",dayList);
        //查询到诊数量
        List<Integer> orderStatus =orderService.findtoOrderStatus((ArrayList<String>) dayList);
        map.put("orderStatus",orderStatus);

        //查询预约数量
        List<Integer> orderList =orderService.findtoOrderList((ArrayList<String>) dayList);
        map.put("orderList",orderList);



        return new Result(true, "查询成功",map);


    }

    @RequestMapping("/findOrderDate")
    public Result findOrderDate() throws Exception {
        try {

            Map<String, Object> map = new HashMap<>();
            ArrayList<String> dayList = new ArrayList<>();


            Calendar calendar = Calendar.getInstance();//获得日历对象，模拟时间就是当前时间
            calendar.add(Calendar.DAY_OF_YEAR, -7);
            for (int i = 0; i < 7; i++) {
                calendar.add(Calendar.DAY_OF_YEAR, 1);
                Date time = calendar.getTime();
                dayList.add(new SimpleDateFormat("yyyy.MM.dd").format(time));
            }
            map.put("dayList", dayList);
            //查询到诊数量
            List<Integer> orderStatus = orderService.findtoOrderStatus(dayList);
            map.put("orderStatus", orderStatus);

            //查询预约数量
            List<Integer> orderList = orderService.findtoOrderList(dayList);
            map.put("orderList", orderList);


            return new Result(true, "查询成功", map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true, "查询失败");
        }
    }
}

